<?php

// functions.php
// Includes all the functions used by multiple pages. 

require("./config/myst.php");

require("./config/access.php");

// ServerStatus() checks the status of all the servers in servers.php and outputs to an array named $s_status.
// For the server status on the front page, checks to make sure all three servers (login, char and map) are reporting
// online. If one is reporting as offline, the front page will report the server as offline. It can also determine
// How many characters are online.
// TODO: Cache these results.
function ServerStatus($s_id) 
{
	require("./config/servers.php");
	$l = @fsockopen($servers[$s_id]['LoginIP'],$servers[$s_id]['LoginPort'],$errno,$errstr,1);
	$c = @fsockopen($servers[$s_id]['CharIP'],$servers[$s_id]['CharPort'],$errno,$errstr,1);
	$m = @fsockopen($servers[$s_id]['MapIP'],$servers[$s_id]['MapPort'],$errno,$errstr,1);
	if ( $l )
	{
		$s_status['l'] = "Online";
	}
	else
	{
		$s_status['l'] = "Offline";
	}
	if ( $c )
	{
		$s_status['c'] = "Online";
	}
	else
	{
		$s_status['c'] = "Offline";
	}
	if ( $m )
	{
		$s_status['m'] = "Online";
	}
	else
	{
		$s_status['m'] = "Offline";
	}
	// Now we need to get the online count from the server
	mysql_connect($servers[$s_id]['DBIP'],$servers[$s_id]['DBLogin'],$servers[$s_id]['DBPass']);
	mysql_select_db($servers[$s_id]['CharMapDB']);
	$q = mysql_query("SELECT * FROM `char` WHERE online = '1'");
	$s_status['o'] = mysql_num_rows($q);
	return $s_status;
}

// Figuring out the title of the page...
function PageTitle()
{

}

// Figuring out a login level...
function GetLevel()
{
	// If session is not set, the user is not logged in, return -1
	if ( !$_SESSION['id'] ) 
	{
		return -1;
	}
	else
	{
		$ServerCount = count($servers);
		$s = $_GET['sid']; // We also need to determine which server we're logging/logged into.
		$ls = $access[$s]['LoginServer'];
		if ( $ls == 0 && $ls <= $ServerCount ) // This value should NEVER equal 0. We can also check here to make sure it's valid by making sure there is no server ID that is greater than it.
		{
			// We need to put a function here that will display an error about invalid settings, because this should never happen.
		}
		else
		{
			mysql_connect($servers[$s]['DBIP'],$servers[$s]['DBUser'],$server[$s]['DBPass']);
			mysql_select_db($servers[$s]['DBName']);
			$query = "SELECT account_id,userid,level
						FROM login
						WHERE account_id = {$_SESSION['id']}";
			mysql_query($query);
			while ( $q2 = mysql_fetch_array($query) )
			{
				extract($q2);
				return $level;
			}
		}
	}
}

// Getting the skin the player has preferenced, and setting the path so it can be retrieved.
function GetSkin()
{

}

//This function is to check to see if a user has logged in. 
// If the user is logged in, then it returns their AID.
// If the user is not logged in, then it returns 0.
// If the user is logged in but banned, it returns -1.
function GetAID() 
{
	if ($_SESSION['auth'] = yes) {
		$aid = mysql_query("SELECT aid FROM login WHERE userid = '{$_SESSION['name']}'");
		$aid2 = mysql_fetch_row();
		$aid2 = $aid2['aid'];
		$isban = mysql_query("SELECT state FROM login WHERE aid = $aid2");
		$isban2 = mysql_fetch_row();
		if ($isban2['state'] > 0)
			return -1;
		else
			return $aid2;
	}
	if ($_SESSION['auth'] = no) {
		return 0;
	}
}

// ReCaptcha() Displays the reCAPTCHA whereever it is needed.
function ReCaptcha() {
	require_once('recaptchalib.php');
	require('./config/myst.php');
	$publickey = $myst['reCaptchaPub']; // you got this from the signup page
	return recaptcha_get_html($publickey);
}

// ReCaptcha_Check() Checks the Captcha input whereever it is needed.
function ReCaptcha_Check() {
	require_once('recaptchalib.php');
	require('./config/myst.php');
	$publickey = $myst['reCaptchaPub'];
	$privatekey = $myst['reCaptchaPrivate']; // you got this from the signup page
	$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

	if (!$resp->is_valid) {
		return false;
	} else {
		return true;
	}	  
}

function GetMD5Key() {
	
}